/*=========================================================================
Elite College as Engines of Upward Mobility: Evidence from Colombia's Ser Pilo Paga
Authors: Juliana Londoño-Vélez, Catherine Rodriguez, Fabio Sánchez
and Luis Esteban Álvarez-Arango

Creation date: June 6th, 2025
--------------------------------------------------------------------------
Table A2 and A4
=========================================================================*/

use "${data}/data_RD", clear
keep if icfes_per==20142
drop if m==1
gen double i_running_saber11 = running_saber11*eligible_saber11
gen double i_running_sisben = running_sisben*eligible_sisben

gen sisben_score_100=sisben_score
replace sisben_score_100=100 if sisben_score==.
xtile percentil_saber11=running_saber11, nq(100)
xtile percentil_sisben=sisben_score_100, nq(100)

local filtros " "" "& graduation_exam_pro_5==1" "
local tables "A2" "A4"

local f=1
foreach x of local filtros{
	local z: word `f' of "`tables'"
	matrix r2014=J(44,18,.)

	cap rdrobust running_sisben running_saber11 if eligible_sisben==1 `x', masspoints(off) 
	if _rc==0{
	matrix r2014[1,1]=e(tau_cl)
	matrix r2014[1,2]=e(ci_r_rb)
	matrix r2014[1,3]=e(ci_l_rb)
	matrix r2014[1,4]=e(tau_cl_l)
	matrix r2014[1,5]=e(h_l)
	matrix r2014[1,6]=e(N)
	matrix r2014[1,7]=e(N_h_l)
	matrix r2014[1,8]=e(N_h_r)
	matrix r2014[1,9]=e(pv_rb)
	}
	cap rdrobust running_saber11 running_sisben if eligible_saber11==1 `x', masspoints(off) 
	if _rc==0{
	matrix r2014[1,10]=e(tau_cl)
	matrix r2014[1,11]=e(ci_r_rb)
	matrix r2014[1,12]=e(ci_l_rb)
	matrix r2014[1,13]=e(tau_cl_l)
	matrix r2014[1,14]=e(h_l)
	matrix r2014[1,15]=e(N)
	matrix r2014[1,16]=e(N_h_l)
	matrix r2014[1,17]=e(N_h_r)
	matrix r2014[1,18]=e(pv_rb)
	}
	local i=2
	foreach o in percentil_saber11 percentil_sisben icfes_student icfes_female icfes_age ethnminority icfes_works family_size icfes_educm1 icfes_educm2 icfes_educm3 icfes_educm4 icfes_educp1 icfes_educp2 icfes_educp3 icfes_educp4 icfes_stratum1 icfes_stratum2 icfes_stratum3 icfes_stratum4 icfes_stratum5 icfes_stratum6 icfes_schoolsch1 icfes_schoolsch2 icfes_schoolsch3 icfes_schoolsch4 icfes_schoolsch5 icfes_privatehs school_schedule_a school_schedule_b school_schedule_other floors_1 floors_2 floors_3 floors_4 family_internet family_laptop family_car family_cell_phone student_resides_urban school_urban {

	cap rdrobust `o' running_saber11 if eligible_sisben==1 `x', masspoints(off) 
	if _rc==0{
	matrix r2014[`i',1]=e(tau_cl)
	matrix r2014[`i',2]=e(ci_r_rb)
	matrix r2014[`i',3]=e(ci_l_rb)
	matrix r2014[`i',4]=e(tau_cl_l)
	matrix r2014[`i',5]=e(h_l)
	matrix r2014[`i',6]=e(N)
	matrix r2014[`i',7]=e(N_h_l)
	matrix r2014[`i',8]=e(N_h_r)
	matrix r2014[`i',9]=e(pv_rb)
	}
	cap rdrobust `o' running_sisben if eligible_saber11==1 `x', masspoints(off) 
	if _rc==0{
	matrix r2014[`i',10]=e(tau_cl)
	matrix r2014[`i',11]=e(ci_r_rb)
	matrix r2014[`i',12]=e(ci_l_rb)
	matrix r2014[`i',13]=e(tau_cl_l)
	matrix r2014[`i',14]=e(h_l)
	matrix r2014[`i',15]=e(N)
	matrix r2014[`i',16]=e(N_h_l)
	matrix r2014[`i',17]=e(N_h_r)
	matrix r2014[`i',18]=e(pv_rb)
	}
	local i=1+`i'
}

global controls "icfes_student icfes_female icfes_age ethnminority icfes_works family_size icfes_educm1 icfes_educm2 icfes_educm3 icfes_educm4 icfes_educp1 icfes_educp2 icfes_educp3 icfes_educp4 icfes_stratum1 icfes_stratum2 icfes_stratum3 icfes_stratum4 icfes_stratum5 icfes_stratum6 icfes_schoolsch1 icfes_schoolsch2 icfes_schoolsch3 icfes_schoolsch4 icfes_schoolsch5 icfes_privatehs school_schedule_a school_schedule_b school_schedule_other floors_1 floors_2 floors_3 floors_4 family_internet family_laptop family_car family_cell_phone student_resides_urban school_urban"
global running_saber11 "running_saber11 if eligible_sisben==1"
global running_sisben "running_sisben if eligible_saber11==1"
* Roughly max/min values from Table 2, BW Loc. Poly. 
global rmax_running_saber11 "running_saber11/40"
global rmin_running_saber11 "running_saber11/20"
global rmax_running_sisben "running_sisben/15"
global rmin_running_sisben "running_sisben/7"
global c_running_saber11 "eligible_saber11 running_saber11 i_running_saber11 ${controls}" 
global c_running_sisben "eligible_sisben running_sisben i_running_sisben ${controls}"

foreach r in saber11 sisben{
	local mm=0
	foreach m in min max{
		if "`r'"=="saber11" local rr=1
		if "`r'"=="sisben" 	local rr=10
		if "`r'"=="saber11" local w="sisben"
		if "`r'"=="sisben" 	local w="saber11"
		capture drop u`m' kw`m'
		gen u`m' = abs(${r`m'_running_`r'})
		gen kw`m' = 1 - u`m'
		replace kw`m' = 0 if u`m' > 1
		reg ${c_running_`r'} percentil_`w' [pw=kw`m'] if eligible_`w'==1 `x'
		testparm ${controls} percentil_`w'
		matrix r2014[43+`mm',`rr']=round(r(p),0.001)
		local mm = `mm'+1
	}
}

***********************************
** Construcción Tablas de resultados
***********************************	
preserve
	clear
	svmat r2014
	gen nombre=""
	order nombre
	rename (r20141 r20142 r20143 r20144 r20145 r20146 r20147 r20148 r20149 r201410 r201411 r201412 r201413 r201414 r201415 r201416 r201417 r201418) ///
	(c_11 cs_11 ci_11 c0_11 bw_11 n_11 ne0_11 ne_11 p_11 c_si cs_si ci_si c0_si bw_si n_si ne0_si ne_si p_si)
	replace nombre="Running variable" in 1
	replace nombre="SABER 11 percentile" in 2
	replace nombre="Wealth percentile (including missing SISBEN)" in 3
	replace nombre="Took the Saber 11 test as a student" in 4
	replace nombre="Female" in 5
	replace nombre="Age" in 6
	replace nombre="Ethnic minority" in 7
	replace nombre="Employed" in 8
	replace nombre="Family size" in 9
	replace nombre="Mother's education: primary" in 10
	replace nombre="Mother's education: secondary" in 11
	replace nombre="Mother's education: T&T" in 12
	replace nombre="Mother's education: professional" in 13
	replace nombre="Father's education: primary" in 14
	replace nombre="Father's education: secondary" in 15
	replace nombre="Father's education: T&T" in 16
	replace nombre="Father's education: professional" in 17	
	replace nombre="Household SES: Stratum 1" in 18
	replace nombre="Household SES: Stratum 2" in 19
	replace nombre="Household SES: Stratum 3" in 20
	replace nombre="Household SES: Stratum 4" in 21
	replace nombre="Household SES: Stratum 5" in 22
	replace nombre="Household SES: Stratum 6" in 23
	replace nombre="School hours: Full day" in 24
	replace nombre="School hours: Morning" in 25
	replace nombre="School hours: Evening" in 26
	replace nombre="School hours: Afternoon" in 27
	replace nombre="School hours: Weekends" in 28
	replace nombre="Private school" in 29
	replace nombre="School schedule: A" in 30
	replace nombre="School schedule: B" in 31
	replace nombre="School schedule: Other" in 32
	replace nombre="Floor: cement/ gravel/ brick" in 33
	replace nombre="Floor: wood, board, wooden plank" in 34
	replace nombre="Floor:polished wood, tile, marble, carpet " in 35
	replace nombre="Floor: land, sand" in 36
	replace nombre="Family has internet" in 37	
	replace nombre="Family has a laptop" in 38
	replace nombre="Family has a car" in 39
	replace nombre="Family has a cellphone" in 40
	replace nombre="Student resides: Urban" in 41
	replace nombre="School location: Urban" in 42
	replace nombre="Joint F-Stat (p-value, LB on bandwidth)" in 43
	replace nombre="Joint F-Stat (p-value, UB on bandwidth)" in 44
	
	foreach v of varlist *11{
		replace `v'=. if nombre=="SABER 11 percentile"
	}
	
	foreach v of varlist *si{
		replace `v'=. if nombre=="Wealth percentile (including missing SISBEN)"
	}
	
	local y=regexr("`x'","& ","")
	local y=regexr("`y'","==1","")

	export excel "${outcomes}/Tab_`z'.xlsx", firstrow(variables)
restore
	local f=`f'+1
}